<template>
	<view>
		<button open-type="contact" bindcontact="handleContact"
		show-message-card session-from='cloudeminiprogram' send-message-title='官方小程序啊'
		send-message-path='pages/index/index' 
		send-message-img='https://vkceyugu.cdn.bspapp.com/VKCEYUGU-62ec721a-a0ff-4726-a9f0-0e206b972095/796891ec-c03b-43c6-a351-a4f78ec964df.jpg'>
		小程序客服</button>
		<button @click="upload">上传临时素材</button>
	</view>
</template>

<script>
	const miniupload = uniCloud.importObject('mini-upload') //第一步导入云对象
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			// 进入小程序客服
			handleContact (e) {
				console.log(e.detail.path)
				console.log(e.detail.query)
			},
			//ArrayBuffer转Buffer
			toBuffer(ab) {
			    var buf = new Buffer(ab.byteLength);
			    var view = new Uint8Array(ab);
			    for (var i = 0; i < buf.length; ++i) {
			        buf[i] = view[i];
			    }
			    return buf;
			},
			getImageInfor(path){
				return new Promise((reslove, reject) => {
					wx.getImageInfo({
					    src:path,		
					    success: function (res) {	
							if(res.errMsg=='getImageInfo:ok'){
								reslove(res.type);
							}else{
								reject(err)
							}
					    },
						fail: (err) => {
							reject(err)
						}
					})
				})
			},
			handlebase64(path,type){
				wx.getFileSystemManager().readFile({
					filePath: path,
					encoding:'base64',
					success:async r => { 
						// console.log(r,r.data)
						let data=await miniupload.upload({filepath:r.data,type})
						console.log(data)
					}
				})
			},
			upload(){  
				uni.chooseImage({
					count: 1, //默认9
					success:async res=> {
						console.log(res,res.tempFilePaths[0]);
						let type=await this.getImageInfor(res.tempFilePaths[0])
						console.log(type,456)
						// 上传base64到云函数，云函数处理为buffer数据
						this.handlebase64(res.tempFilePaths[0],type)
						// 上传buffer数据到云函数，云函数再处理
						// wx.getFileSystemManager().readFile({
						// 	filePath: res.tempFilePaths[0],
						// 	success:async r => { 
						// 		// console.log(r,r.data)
						// 		let buffer=this.toBuffer(r.data)
						// 		let data=await miniupload.upload({filepath:buffer,type})
						// 		console.log(data)
						// 	}
						// })
					}
				});
			},
		}
	}
</script>

<style>

</style>
